<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use app\util\File;
use app\util\Utils;
use app\util\Callout;
use think\Db;
use app\index\model\Daping;
use app\index\model\ProvCityArea;
use app\index\model\Store;
use app\index\model\StoreCtm;
use app\index\model\Custom;
use app\index\model\StaffBindStore;
use app\index\model\Order;
use app\index\model\Yuyue;
use app\index\model\Item;

class PublicController extends Controller {
    /*
     * 上传图片
     */

    public function uploadImg() {
        $modename = input('param.modeName');
        //上传头像
        $uploadFile = request()->file('file');
        if ($uploadFile) {
            //验证图片
            $file = new File();
            if (!$file->checkImg($uploadFile)) {
                $error = $file->getError();
                return Utils::ajaxReturn(1, $error, null);
            }
        } else {
            return Utils::ajaxReturn(3, '图片必须', null);
        }
        $photo = $file->uploadImage($uploadFile, $modename . '/');
        $mode = config('appconfig.app_production');
        if ($mode) {
            $publicFolder = 'kkmimg';
        } else {
            $publicFolder = 'testkkmimg';
        }
        $realPhoto = $file->getImageRealPath($photo);
        $curlPhoto = $file->curlUploadImage($publicFolder, pathinfo($photo, PATHINFO_DIRNAME), false, $realPhoto);
        if (!$curlPhoto) {
            return Utils::ajaxReturn(3, '图片上传失败', null);
        } else {
            return Utils::ajaxReturn(0, '上传成功', config('appconfig.ImgPath') . $publicFolder . '/' . $photo);
        }
    }

    /*
     * 上传base64图片
     */

    public function uploadBase64Img() {
        $folder = input('param.modeName');
        //上传头像
        $base64_img = trim(input('param.img'));
        if (empty($base64_img)) {
            return Utils::ajaxReturn(3, '请上传图片', null);
        }
        $base64_img = 'data:image/jpg;base64,' . $base64_img;
        $file = new File();
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)) {
            $type = $result[2];
            if (in_array($type, array('pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png'))) {
                $mode = config('appconfig.app_production');
                if ($mode) {
                    $publicFolder = 'kkmimg';
                } else {
                    $publicFolder = 'testkkmimg';
                }
                $path = ROOT_PATH . '../' . $publicFolder;
                $path2 = '/kkmai/' . $folder . '/' . date('Ymd') . '/' . time() . substr(microtime(), 2, 4) . mt_rand(1000, 9999) . '.' . $type;
                $savename = $path . $path2;
                $dirPath = $path . '/kkmai/' . $folder . '/' . date('Ymd') . '/';
                if (!is_dir($dirPath)) {
                    $flag = mkdir($dirPath, 0777, true);
                }
                if (file_put_contents($savename, base64_decode(str_replace($result[1], '', $base64_img)))) {
                    //echo "上传成功";
                } else {
                    return Utils::ajaxReturn(3, '图片上传失败', null);
                }
            } else {
                return Utils::ajaxReturn(3, '图片上传类型错误', null);
            }
        } else {
            return Utils::ajaxReturn(3, '文件错误', null);
        }

        $curlPhoto = $file->curlUploadImage($publicFolder, pathinfo($path2, PATHINFO_DIRNAME), false, $savename);
        if (!$curlPhoto) {
            return Utils::ajaxReturn(3, '图片上传失败', null);
        } else {
            return Utils::ajaxReturn(0, '上传成功', config('appconfig.ImgPath') . $publicFolder . $path2);
        }
    }

    /*
     * 生成预约确认码
     */

    public function yuyueQrcode() {
        $code = input('param.code');

        $file = new File();
        $photo = $file->generateCtmStaffQrcode($code);
        $mode = config('appconfig.app_production');
        if ($mode) {
            $publicFolder = 'kkmimg';
        } else {
            $publicFolder = 'testkkmimg';
        }
        if (!$photo) {
            return Utils::ajaxReturn(3, '图片生成失败', null);
        } else {
            return Utils::ajaxReturn(0, '生成成功', config('appconfig.ImgPath') . $publicFolder . '/' . $photo);
        }
    }

    /*
     * 虚拟电话绑定
     * author xfwed
     * date 2019.10.09
     */

    public function midNumBind() {//$midNum,$phone,$expiration
        $phone = input('param.phone');
        //return Utils::ajaxReturn(0, '绑定成功', $phone);
        $midNum = '16210500865';//17081082707  15669475259
        $expiration = 180;
        $Callout = new Callout();
        $result = $Callout->midNum($midNum, $phone, $expiration);

        $result = json_decode($result, true);
        $status = $result['success'];
        if ($status) {
            return Utils::ajaxReturn(0, '绑定成功', $midNum);
        } else {
            $message = $result['message'];
            //return Utils::ajaxReturn(1, $message, $result);
            return Utils::ajaxReturn(0, '绑定成功', $phone);
        }
    }

    /*
     * 虚拟电话解绑
     * author xfwed
     * date 2019.10.09
     */

    public function midNumUnBinding() {//$mappingId,$midNum
        $mappingId = 12566;
        $midNum = input('param.phone');
        $Callout = new Callout();
        $result = $Callout->midNumUnBinding($mappingId, $midNum);

        $result = json_decode($result, true);
        $status = $result['success'];
        if ($status) {
            return Utils::ajaxReturn(0, '解绑成功', $result);
        } else {
            $message = $result['message'];
            return Utils::ajaxReturn(1, $message, $result);
        }
    }

    /*
     * 大屏数据
     * author xfwed
     * date 2020.06.22
     */

    public function dapingData() {
        //用户来源
        $leftTopData = Daping::order('id desc')
                ->field('id,rightTop1,rightTop2,rightTop3,rightTop4,rightTop5,rightTop6,rightTop7,rightTop8,rightTop9,rightTop10,rightDown1,rightDown2,rightDown3,rightDown4', true)
                ->find();
        $data['leftTop'] = $leftTopData;
        //近12个月的订单量
        $orderList = Order::whereTime('pay_time', '-11 month')
                ->where('status', 2)
                ->group("DATE_FORMAT(pay_time,'%m')")
                ->order('pay_time asc')
                ->field("DATE_FORMAT(pay_time, '%m') AS m,count(id) AS c")
                ->select();
        $data['orderList'] = $orderList;
//        //活跃用户
//        $p = ProvCityArea::where('level',1)->field('id,name')->select();
//        $ctmAllNum = Custom::where('status',2)->count();
//        foreach ($p as $k => $v){
//            $storeIds = Store::where('is_del',1)->where('prov',$v['id'])->column('id');
//            $ctmNum = StoreCtm::where('store_id','in',$storeIds)->group('ctm_id')->count();
//            $p[$k]['per'] = bcdiv($ctmNum,$ctmAllNum,2)*100;
//        }
//        $data['provCtmNum'] = $p;
//        //城市用户
//        $c = ProvCityArea::where('level',2)->field('id,name')->select();
//        foreach ($c as $k => $v){
//            $storeIds = Store::where('is_del',1)->where('city',$v['id'])->column('id');
//            $ctmNum = StoreCtm::where('store_id','in',$storeIds)->group('ctm_id')->count();
//            $c[$k]['per'] = bcdiv($ctmNum,$ctmAllNum,2)*100;
//        }
//        $data['cityCtmNum'] = $c;
        //地图数据
        //无人店数据
        $newStoreList = Store::where('is_del', 1)
                        ->field('id,store_name,city,money,tx_money,longitude,latitude,add_time')
                        ->paginate(10000)
                        ->each(function($item, $key) {
                            $item['cityName'] = ProvCityArea::where('id', $item['city'])->value('name');
                            //$item['newPeople'] = Order::where('store_id', $item['id'])->where('first_exemption', 2)->where('status', 2)->count();
                            //$item['miaoshaNum'] = Order::where('store_id', $item['id'])->where('type', 5)->where('status', 2)->count();
                            //$item['special'] = Order::where('store_id', $item['id'])->where('type', 4)->where('status', 2)->count();
                            //$item['imeiyy'] = Custom::where('smallshop_storeid', $item['id'])->count();
                            //$item['ctmNum'] = StoreCtm::where('store_id', $item['id'])->group('ctm_id')->count();
                            //$item['storeMoney'] = $item['money']/100+$item['tx_money']/100;
                            $item['cp'] = [$item['longitude'], $item['latitude']];
                            //$storeList = Store::where('city', $item['city'])->where('is_del', 1)->where('auditstore', 1)->order('id asc')->column('id');
                            //$cityRanking = array_keys($storeList, $item['id'])[0];
                            //$item['cityRanking'] = $cityRanking + 1;
                            //本市的其他店铺
                            $cityStoreList = Store::where('city',$item['city'])
                                    ->where('is_del',1)
                                    ->where('auditstore', 1)
                                    ->orderRaw('id='.$item['id'].' desc')
                                    ->field('store_name')
                                    ->select();
                            $item['cityStoreList'] = $cityStoreList;
                            unset($item['city']);
                            unset($item['longitude']);
                            unset($item['latitude']);
                            unset($item['money']);
                            unset($item['tx_money']);
                        })->toArray();
        $data['newStoreList'] = $newStoreList['data'];
        //粉色版店铺
        $oldStoreList = Db::connect($this->database())
                        ->table('da_store')
                        ->where('auditstore', '=', 1)
                        ->where('is_del', '=', 1)
                        ->field('id,longitude,latitude')
                        ->paginate(1000000)->toArray();
        $oldStoreList = $oldStoreList['data'];
        foreach ($oldStoreList as $k => $v) {
            $oldStoreList[$k]['name'] = $v['id'];
            $oldStoreList[$k]['cp'] = [$v['longitude'], $v['latitude']];
            unset($oldStoreList[$k]['id']);
            unset($oldStoreList[$k]['longitude']);
            unset($oldStoreList[$k]['latitude']);
        }
        $data['oldStoreList'] = $oldStoreList;
        //地图左下角用户数量
        $oldCtmNum = Db::connect($this->database())->table('da_custom')->where('status', 2)->count();
        $newCtmNum = Custom::where('status', 2)->count();
        $data['ctmNum'] = ($oldCtmNum + $newCtmNum) * 3;
        //地图左下角入驻商家
        $oldStoreNum = Db::connect($this->database())->table('da_store')->where('auditstore', '=', 1)->where('is_del', '=', 1)->count();
        $data['oldStoreNum'] = $oldStoreNum * 2;
        //地图左下角直营连锁
        $newStoreNum = Store::where('is_del', 1)->count();
        $data['newStoreNum'] = ceil($newStoreNum * 1.5);
        //地图左下角总交易额
        $orderwhere['pay_money'] = array('>', 0);
        $orderwhere['status'] = array('>', 1);
        $orderwhere['ordtype'] = array('in', '1,3,4,5,6,7');
        $orderwhere['store_id'] = array('<>', '162');
        $oldStoreMoney = Db::connect($this->database())->table('da_order')
                ->where($orderwhere)
                ->sum('pay_money');
        $tuikuan = Yuyue::where('status', '=', 6)->sum('money'); //退款金额
        $payMoney = Order::where('status', '>=', 2)->sum('pay_money'); //订单金额
        $newStoreMoney = $payMoney - $tuikuan;
        $data['storeMoney'] = ($oldStoreMoney + $newStoreMoney / 100) * 3;
        //用户画像
        $rightTopData = Daping::order('id desc')
                ->field('rightTop2,rightTop3,rightTop4,rightTop5,rightTop6,rightTop7,rightTop8,rightTop9')
                ->find();
        $data['rightTop'] = $rightTopData;
        //访问数据--上月
        $oldTime = date("Y-m-d H:i:s",strtotime("-30 day"));
        $visitNum = StoreCtm::where('add_time','>=',$oldTime)
                ->group("DATE_FORMAT(add_time,'%H')")
                ->order('add_time asc')
                ->field("DATE_FORMAT(add_time, '%H') AS h,count(id) AS c")
                ->select();
        foreach ($visitNum as $k => $v) {
            $h[] = $v['h'];
        }
        $visitNum0 = [];
        for ($i = 0; $i <= 23; $i++) {
            if (!in_array($i, $h)) {
                if ($i < 10) {
                    $visitNum0[$i]['h'] = '0' . "$i";
                } else {
                    $visitNum0[$i]['h'] = "$i";
                }
                $visitNum0[$i]['c'] = 0;
            }
        }
        $c = array_merge($visitNum, $visitNum0);
        $last_names = array_column($c, 'h');
        array_multisort($last_names, SORT_ASC, $c);
        $data['visitNum'] = $c;

//        //访问数据--今日
//        $todayVisitNum = StoreCtm::whereTime('add_time', 'today')
//                ->group("DATE_FORMAT(add_time,'%H')")
//                ->order('add_time asc')
//                ->field("DATE_FORMAT(add_time, '%H') AS h,count(id) AS c")
//                ->select();
//        foreach ($todayVisitNum as $k => $v) {
//            $t[] = $v['h'];
//            $todayVisitNum[$k]['c'] = $v['c'];
//        }
//        $todayVisitNum0 = [];
//        for ($i = 0; $i <= 23; $i++) {
//            if (!in_array($i, $t)) {
//                if ($i < 10) {
//                    $todayVisitNum0[$i]['h'] = '0' . "$i";
//                } else {
//                    $todayVisitNum0[$i]['h'] = "$i";
//                }
//                $todayVisitNum0[$i]['c'] = 0;
//            }
//        }
//        $ctoday = array_merge($todayVisitNum, $todayVisitNum0);
//        $last_names = array_column($ctoday, 'h');
//        array_multisort($last_names, SORT_ASC, $ctoday);
//        $data['todayVisitNum'] = $ctoday;
//
//        //访问数据--昨日
//        $yesterdayVisitNum = StoreCtm::whereTime('add_time', 'yesterday')
//                ->group("DATE_FORMAT(add_time,'%H')")
//                ->order('add_time asc')
//                ->field("DATE_FORMAT(add_time, '%H') AS h,count(id) AS c")
//                ->select();
//        foreach ($yesterdayVisitNum as $k => $v) {
//            $y[] = $v['h'];
//            $yesterdayVisitNum[$k]['c'] = $v['c'];
//        }
//        $yesterdayVisitNum0 = [];
//        for ($i = 0; $i <= 23; $i++) {
//            if (!in_array($i, $y)) {
//                if ($i < 10) {
//                    $yesterdayVisitNum0[$i]['h'] = '0' . "$i";
//                } else {
//                    $yesterdayVisitNum0[$i]['h'] = "$i";
//                }
//                $yesterdayVisitNum0[$i]['c'] = 0;
//            }
//        }
//        $cyesterday = array_merge($yesterdayVisitNum, $yesterdayVisitNum0);
//        $last_names = array_column($cyesterday, 'h');
//        array_multisort($last_names, SORT_ASC, $cyesterday);
//        $data['yesterdayVisitNum'] = $cyesterday;

        //访问数据--本周
        $oldTime = date("Y-m-d H:i:s",strtotime("-7 day"));
        $weekVisitNum = StoreCtm::whereTime('add_time','>=',$oldTime)//whereTime('add_time', '-1 week')
                ->group("DATE_FORMAT(add_time,'%H')")
                ->order('add_time asc')
                ->field("DATE_FORMAT(add_time, '%H') AS h,count(id) AS c")
                ->select();
        
        foreach ($weekVisitNum as $k => $v) {
            $w[] = $v['h'];
        }
        $weekVisitNum0 = [];
        for ($i = 0; $i <= 23; $i++) {
            if (!in_array($i, $w)) {
                if ($i < 10) {
                    $weekVisitNum0[$i]['h'] = '0' . "$i";
                } else {
                    $weekVisitNum0[$i]['h'] = "$i";
                }
                $weekVisitNum0[$i]['c'] = 0;
            }
        }
        $cweek = array_merge($weekVisitNum, $weekVisitNum0);
        $last_names = array_column($cweek, 'h');
        array_multisort($last_names, SORT_ASC, $cweek);
        $data['weekVisitNum'] = $cweek;
        //近30天销售统计
        //销售金额
        $payMoney30 = Order::whereTime('pay_time', '-1 month')
                ->where('status', 2)
                ->sum('pay_money');
        $data['payMoney30'] = $payMoney30;
        //成交量
        $ordNum30 = Order::whereTime('pay_time', '-1 month')
                ->where('status', 2)
                ->count();
        $data['ordNum30'] = $ordNum30;
        //客单均价
        $ctmAvg30 = Order::whereTime('pay_time', '-1 month')
                ->where('status', 2)
                ->avg('pay_money');
        $data['ctmAvg30'] = ceil($ctmAvg30);

        //营销趋势//
        //商品销售TOP榜
        $goodsTop3 = Db::table('goods_info')->where('is_smallshopsell', 1)->field('name,sell_count')->order('sell_count desc')->limit(0, 3)->select();
        $data['goodsTop3'] = $goodsTop3;
        //项目销售TOP榜
        $itemTop3 = Item::field('item_name,sell_count')->order('sell_count desc')->limit(0, 3)->select();
        $data['itemTop3'] = $itemTop3;
        //当月明星产品
        //$beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
        //$endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        //$beginThismonth = date('Y-m-d H:i:s', $beginThismonth);
        //$endThismonth = date('Y-m-d H:i:s', $endThismonth);
        $beginThismonth = date("Y-m-d H:i:s",strtotime("-30 day"));
        $endThismonth = date("Y-m-d H:i:s");
        $mgoods1 = Db::table('pay_order_detail')->alias('o')
                ->join(['pay_order_goods' => 'g'], 'g.pay_order_id = o.id', 'left')
                ->where('status', 'in', '2,3,4')
                ->where('o.pay_time', 'between', [$beginThismonth, $endThismonth])
                ->group('g.goods_id')
                ->field('g.name,g.goods_icon,sum(g.number) num')
                ->order('num desc')
                ->limit(0, 1)
                ->select();
        $data['mgoods1'] = $mgoods1;
        //当月明星项目
        $mItem1 = Order::alias('o')
                ->join('ord_pro p', 'p.ord_id = o.id', 'left')
                ->join('item i', 'i.id = p.pro_id', 'left')
                ->where('o.type', 1)
                ->where('o.status', 2)
                ->where('o.pay_time', 'between', [$beginThismonth, $endThismonth])
                ->group('p.pro_id')
                ->field('i.item_name,i.item_img,sum(p.number) num')
                ->order('num desc')
                ->limit(0, 1)
                ->select();
        $data['mItem1'] = $mItem1;

        //客流分析
        $ctmNum = Custom::where('status', 2)->count();
        $vipCtmNum = Custom::where('status', 2)->where('vip_status', 2)->count();
        $newCtmNum = Custom::where('status', 2)->where('vip_status', 0)->count();
        $oldCtmNum = Custom::where('status', 2)->where('vip_status', 1)->count();
        $data['vipCtmNum'] = bcmul(bcdiv($vipCtmNum, $ctmNum, 2), 100, 0);
        $data['newCtmNum'] = bcmul(bcdiv($newCtmNum, $ctmNum, 2), 100, 0);
        $data['oldCtmNum'] = 100 - $data['vipCtmNum'] - $data['newCtmNum'];

        //肤质情况
        $rightDownData = Daping::order('id desc')
                ->field('id,rightDown1,rightDown2,rightDown3,rightDown4')
                ->find();
        $data['rightDown'] = $rightDownData;

        //当月美容师排名
        $sql = "SELECT 
                t1.id,
                IF(t1.nickname IS NULL,t1.staff_name,t1.nickname) staffName,
                t1.telephone AS staffPhone,t1.staff_photo,t1.city,

                ((SELECT IFNULL(SUM(CASE IFNULL(staff_star,'5') WHEN '1' THEN -5 WHEN '2' THEN -3 WHEN '3' THEN 0 WHEN '4'
                THEN 1 WHEN '5' THEN 3 END),0)
                FROM ai_comment WHERE staff_id=t1.id AND add_time>DATE_ADD(NOW(),INTERVAL -30 DAY)
                )+
                ( SELECT COUNT(id)*0.5 FROM ai_staff_link WHERE staff_id = t1.id AND add_time>DATE_ADD(NOW(),INTERVAL -30
                DAY))+
                (SELECT COUNT(id) FROM ai_yuyue WHERE staff_id=t1.id AND add_time>DATE_ADD(NOW(),INTERVAL -30 DAY) AND
                STATUS IN (3,4,7,8))+
                (SELECT COUNT(id)*-3 FROM ai_yuyue WHERE staff_id=t1.id AND add_time>DATE_ADD(NOW(),INTERVAL -30 DAY) AND
                STATUS IN (5,10))+
                (SELECT COUNT(id) FROM ai_ctm_collect WHERE TYPE=2 AND pro_id=t1.id AND add_time>DATE_ADD(NOW(),INTERVAL -30
                DAY))+
                (SELECT COUNT(1) FROM (
                SELECT id,staff_id FROM ai_store_ctm WHERE add_time>DATE_ADD(NOW(),INTERVAL -30 DAY)
                GROUP BY ctm_id,staff_id,TYPE,DATE_FORMAT(add_time,'%Y%m%d')
                ) a WHERE a.staff_id=t1.id)) AS collectNum

                FROM ai_staff t1
                WHERE t1.is_job = 1 AND t1.staff_on = 1 
                ORDER BY collectNum DESC
                LIMIT 0,3";
        $staffTop3 = Db::query($sql);
        foreach ($staffTop3 as $k => $v){
            $areaName = ProvCityArea::where('id',$v['city'])->value('merger_name');
            $areaName = explode(',',$areaName);
            array_shift($areaName);
            $staffTop3[$k]['areaName'] =  join(',',$areaName);
        }
        $data['staffTop3'] = $staffTop3;
        return Utils::ajaxReturn(0, '查询成功', $data);
    }

    /*
     * 大屏数据-城市门店
     * author xfwed
     * date 2020.08.27
     */

    public function dapingStoreData() {
        //注册新用户 购买  接单  服务  店铺当天操作最近的一条数据  当天都没有数据的显示访问次数
        $today_start = date("Y-m-d 00:00:00");
        $today_end = date("Y-m-d 23:59:59");
        $storeList = Store::where('auditstore', 1)->where('is_del', 1)->field('id,city,store_name')->select();
        foreach ($storeList as $k => $v) {
            $data = [];
            $storeList[$k]['cityName'] = ProvCityArea::where('id', $v['city'])->value('name');
            $ctmRow = Custom::where('source_type', 3)
                    ->where('source_id', $v['id'])
                    ->where('add_time', 'between', [$today_start, $today_end])
                    ->order('add_time desc')
                    ->field('custom_name,add_time')
                    ->find();
            if ($ctmRow) {
                $data[] = $ctmRow['add_time'];
            }
            $orderRow = Order::where('store_id', $v['id'])
                    ->where('status', 2)
                    ->where('add_time', 'between', [$today_start, $today_end])
                    ->order('add_time desc')
                    ->field('id,type,ctm_id,pay_time add_time')
                    ->find();
            if ($orderRow) {
                $data[] = $orderRow['add_time'];
            }
            $yuyueRow3 = Yuyue::where('store_id', $v['id'])
                    ->where('status', '=', 3)
                    ->where('confirm_time', 'between', [$today_start, $today_end])
                    ->order('confirm_time desc')
                    ->field('staff_id,item_id,confirm_time add_time')
                    ->find();
            if ($yuyueRow3) {
                $data[] = $yuyueRow3['add_time'];
            }
            $yuyueRow4 = Yuyue::where('store_id', $v['id'])
                    ->where('status', '=', 4)
                    ->where('staff_time', 'between', [$today_start, $today_end])
                    ->order('staff_time desc')
                    ->field('staff_id,item_id,staff_time add_time')
                    ->find();
            if ($yuyueRow4) {
                $data[] = $yuyueRow4['add_time'];
            }
            if (empty($data)) {
                $addTime = null;
            } else {
                rsort($data);
                $addTime = $data[0];
            }

            if ($addTime) {
                if ($addTime == $ctmRow['add_time']) {
                    $note = $ctmRow['custom_name'] . "新用户已注册";
                } elseif ($addTime == $orderRow['add_time']) {
                    $ctmName = Custom::where('id', $orderRow['ctm_id'])->value('custom_name');
                    if ($orderRow['type'] == 1) {
                        $item_name = Db::name('ord_pro')->alias('op')
                                ->join('item i', 'i.id = op.pro_id', 'left')
                                ->where('op.ord_id', $orderRow['id'])
                                ->value('i.item_name');
                    } elseif ($orderRow['type'] == 2) {
                        $item_name = "超值组合";
                    } elseif ($orderRow['type'] == 3) {
                        $item_name = "充值了余额";
                    } elseif ($orderRow['type'] == 4) {
                        $item_name = "99特权卡";
                    } elseif ($orderRow['type'] == 5) {
                        $item_name = "秒杀项目";
                    }
                    $note = $ctmName . "已购买" . $item_name;
                } elseif ($addTime == $yuyueRow3['add_time']) {
                    $staffName = Db::name('staff')->where('id', $yuyueRow3['staff_id'])->value('staff_name');
                    $note = $staffName . "美容师已接单";
                } elseif ($addTime == $yuyueRow4['add_time']) {
                    $staffName = Db::name('staff')->where('id', $yuyueRow4['staff_id'])->value('staff_name');
                    $itemName = Item::where('id', $yuyueRow4['item_id'])->value('item_name');
                    $note = $staffName . "美容师服务" . $itemName;
                }
                $addTime = explode(' ',$addTime);
                array_shift($addTime);
                $storeList[$k]['add_time'] = join(' ',$addTime);
                $storeList[$k]['note'] = $note;
            } else {
                $num = StoreCtm::where('store_id', $v['id'])
                        ->where('add_time', 'between', [$today_start, $today_end])
                        //->group('ctm_id')
                        ->count();
                $note = "今日访问次数" . $num . "次";
                $storeList[$k]['note'] = $note;
            }
        }

        return Utils::ajaxReturn(0, '查询成功', $storeList);
    }

    /*
     * 数据库链接
     */

    public function database() {
        $app_production = config('appconfig.app_production');
        if ($app_production) {
            $databasename = 'kkm20170906';
        } else {
            $databasename = 'kkm20170906test';
        }
        return $database = [
            'type' => 'mysql',
            'dsn' => '',
            'hostname' => 'localhost',
            'database' => $databasename,
            'username' => 'root',
            'password' => 'eDKmpHzrfkcKrHavT5NLuQ',
            'hostport' => '',
            'params' => [],
            'charset' => 'utf8',
            'prefix' => 'da_',
        ];
    }

}
